package com.ztc.ZtcCorporation.dao;
import com.ztc.ZtcCorporation.bean.ZtcCorporation;
import com.ztc.ZtcCorporation.dao.ZtcCorporationDao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.aqgj.util.BaseDaoImpl;

/**
 * <p>
 * 
 * </p>
 * 
 * @ClassName ZtcCorporationDao
 * @JDK 1.5
 * @author
 * @date
 */
@Repository("ZtcCorporationDao")
public class ZtcCorporationDao extends BaseDaoImpl<ZtcCorporation> {
	public String save(ZtcCorporation ztcCorporation) {
		ZtcCorporation test = this.findUniqueBy("name", ztcCorporation.getName());
		if (test != null) {
			return null;
		} else {
			this.getHibernateTemplate().save(ztcCorporation);
			return ztcCorporation.getId();
		}
	}

	public List<ZtcCorporation> getAllZtcCorporations() {
		return this.getAll();
	}

	public String delete(String id) {
		String sql = "delete from ztc_corporation where 1=1";
		if (id != null) {
			sql += " and id='" + id + "'";
		}
		if (this.currentSession().createNativeQuery(sql).executeUpdate() >= 0) {
			return "1";
		}
		return "0";
	}

	public List<ZtcCorporation> searchInfoEquals(ZtcCorporation ztcCorporation) {
		String sql = "select * from ztc_corporation where 1=1";
		if (ztcCorporation.getName() != null) {
			sql += " and name='" + ztcCorporation.getName() + "'";
		}
		if (ztcCorporation.getAddress() != null) {
			sql += " and address='" + ztcCorporation.getAddress() + "'";
		}
		if (ztcCorporation.getPhone() != null) {
			sql += " and phone='" + ztcCorporation.getPhone() + "'";
		}
		if (ztcCorporation.getType() != null) {
			sql += " and type='" + ztcCorporation.getType() + "'";
		}
		if (ztcCorporation.getAreaCode() != null) {
			sql += " and area_code='" + ztcCorporation.getAreaCode() + "'";
		}
		if (ztcCorporation.getUserId() != null) {
			sql += " and user_id='" + ztcCorporation.getUserId() + "'";
		}
		List<ZtcCorporation> list = this.currentSession().createNativeQuery(sql,ZtcCorporation.class).getResultList();
		return list;
	}

	public List<ZtcCorporation> searchInfoLikes(ZtcCorporation ztcCorporation) {
		String sql = "select * from ztc_corporation where 1=1";
		if (ztcCorporation.getName() != null) {
			sql += " and name like '%" + ztcCorporation.getName() + "%'";
		}
		if (ztcCorporation.getAddress() != null) {
			sql += " and address like '%" + ztcCorporation.getAddress() + "%'";
		}
		if (ztcCorporation.getPhone() != null) {
			sql += " and phone like '%" + ztcCorporation.getPhone() + "%'";
		}
		if (ztcCorporation.getUserId() != null) {
			sql += " and user_id like '%" + ztcCorporation.getUserId() + "%'";
		}
		List<ZtcCorporation> list = this.currentSession().createNativeQuery(sql,ZtcCorporation.class).getResultList();
		return list;
	}

	public String update(ZtcCorporation ztcCorporation) {
		ZtcCorporation ztcCorporationBean = this.findUniqueBy("id", ztcCorporation.getId());
		ztcCorporationBean.setId(ztcCorporation.getId());
		if (ztcCorporation.getName() != null) {
			ztcCorporationBean.setName(ztcCorporation.getName());
		}
		if (ztcCorporation.getAddress() != null) {
			ztcCorporationBean.setAddress(ztcCorporation.getAddress());
		}
		if (ztcCorporation.getPhone() != null) {
			ztcCorporationBean.setPhone(ztcCorporation.getPhone());
		}
		if (ztcCorporation.getType() != null) {
			ztcCorporationBean.setType(ztcCorporation.getType());
		}
		if (ztcCorporation.getAreaCode() != null) {
			ztcCorporationBean.setAreaCode(ztcCorporation.getAreaCode());
		}
		if (ztcCorporation.getUserId() != null) {
			ztcCorporationBean.setUserId(ztcCorporation.getUserId());
		}
		this.save(ztcCorporationBean);
		if (ztcCorporationBean.getId() != null) {
			return "1";
		}
		return "0";
	}
}